package com.cloud.code;

class TreeNode {
    public int data;
    public TreeNode left;
    public TreeNode right;

    TreeNode(int data) {
        this.data = data;
    }
}

class TestTree {

    public static void main(String[] args) {
        int[] a=new int[]{62,88,58,47,35,73,51,99,37,93};
        TreeNode root=new TreeNode(a[0]);
        for(int i=1; i<a.length; i++){
            AddTreeNode1(root, a[i]);
        }
        System.out.println("中序遍历结果为：");
        midSort(root);
        System.out.println("47存在："+SearchTreeNode(root,47));
        AddTreeNode1(root,100);
        System.out.println("添加100后，中序遍历结果为：");
        midSort(root);
        System.out.println("添加100后，100存在："+SearchTreeNode(root,100));
    }

    //添加节点
    public static boolean AddTreeNode1(TreeNode root, int data) {
        TreeNode treeNode = new TreeNode(data);
        //树为空
        if (root == null) {
            root = treeNode;
            return true;
        }
        //比根节点小，插入到左子树
        if (root.data > data) {
            if (root.left == null) {
                root.left = treeNode;
                return true;
            } else
                return AddTreeNode1(root.left, data);
        } else if (root.data < data) {       //比根节点大，插入到右子树
            if (root.right == null) {
                root.right = treeNode;
                return true;
            } else
                return AddTreeNode1(root.right, data);
        } else
            return false;
    }

    public static boolean SearchTreeNode(TreeNode root, int data) {
        if (root == null) {
            return false;
        } else if (root.data == data) {
            return true;
        } else if (root.data > data) {
            return SearchTreeNode(root.left, data);
        } else {
            return SearchTreeNode(root.right, data);
        }
    }

    //二叉树中序遍历
    public static void midSort(TreeNode root) {
        if (root == null) {
            return;
        }
        midSort(root.left);
        System.out.print(root.data + " ");
        midSort(root.right);
    }

    //删除
    public static boolean DeleteNode(TreeNode root, int data){
        //current为查找得到的节点
        TreeNode current=root;
        //parent为时刻更新父节点
        TreeNode parent=root;
        //tempParent为同时存在左右子树的迭代临时父节点
        TreeNode tempParent=root;
        //isLeft记录current节点的左右属性
        boolean isLeft=true;
        while(current.data!=data){
            parent=current;
            //到左子树查找
            if(current.data>data){
                isLeft=true;
                current=current.left;
            }else if(current.data<data){ //到右子树查找
                isLeft=false;
                current=current.right;
            }
            //查不到，返回false
            if(current==null)
                return false;
        }
        //第一种情况：删除节点为叶节点
        if(current.left==null && current.right==null){
            if(current==root)
                root=null;
            if(isLeft) {
                parent.left = null;
            }else{
                parent.right = null;
            }
            return true;
        }else if(current.right==null){    //第二种情况：删除节点只有左节点
            if(current==root)
                root=current.left;
            else if(isLeft)
                parent.left=current.left;
            else
                parent.right=current.left;
            return true;
        }else if(current.left==null){    //第三种情况：删除节点只有右节点
            if(current==root)
                root=current.right;
            else if(isLeft)
                parent.left=current.right;
            else
                parent.right=current.right;
            return true;
        }else{  //第四种情况：删除节点均存在左节点和右节点
            if(current==root){
                root=root.left;
            }
            TreeNode tempNode=current.left;
            //没有左节点
            if(tempNode.right==null){
                if(isLeft)
                    parent.left=tempNode;
                else
                    parent.right=tempNode;
            }else{  //存在左节点，迭代到最右侧子节点，即直接前驱
                while(tempNode.right!=null){
                    tempParent=tempNode;
                    tempNode=tempNode.right;
                }
                if(isLeft){    //为左节点，连接
                    parent.left=tempNode;
                    parent.left.left=current.left;
                }else{  //为右节点，连接
                    parent.right=tempNode;
                    parent.right.left=current.left;
                }
                //删除前驱节点，连接
                if(tempNode.left==null)
                    tempParent.right=null;
                else
                    tempParent.right=tempNode.left;
            }
            return true;
        }
    }
}